package com.qiwx.pigeon.fragment.produce;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.adapter.produce.GetOutReccordAdapter;
import com.qiwx.pigeon.base.BaseMVPFragment;
import com.qiwx.pigeon.data.db.SummaryGoOutModel;
import com.qiwx.pigeon.data.db.WareHouseModel;
import com.qiwx.pigeon.data.network.BaseResponse;
import com.qiwx.pigeon.data.vo.GetOutReccordBean;
import com.qiwx.pigeon.data.vo.GoOutBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.fragment.AbsQueryFragment;
import com.qiwx.pigeon.fragment.ProduceFragment;
import com.qiwx.pigeon.presenter.GoOutPresenter;
import com.qiwx.pigeon.viewinterface.IGoOutView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.litepal.LitePal;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_START;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-29 16:52
 * 出栏工段
 **/
public class GetOutOperationFragment extends BaseMVPFragment<IGoOutView, GoOutPresenter> implements IGoOutView {
    @BindView(R.id.edt_getout_amount)
    EditText edtGetoutAmount;
    @BindView(R.id.txt_start_date)
    TextView txtStartDate;
    @BindView(R.id.btn_add)
    Button btnAdd;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    GetOutReccordAdapter adapter;
    List<GoOutBean> data = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_getout;
    }

    @Override
    public void initAfterOnCreate() {
        super.initAfterOnCreate();
        initView();
        getGoOutRecord(MainActivity.wh_id);
    }

    @Override
    public void onReFreshData() {
        super.onReFreshData();
        if(mPresenter==null)
            return;
        getGoOutRecord(MainActivity.wh_id);
    }

    @Override
    public GoOutPresenter createPresenter() {
        return new GoOutPresenter(new WeakReference(this));
    }

    private void initView() {
        txtStartDate.setText(DateTimeUtils.date2String(new Date()));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.addItemDecoration(new LineItemDecoration(2));

    }

    //获取出栏记录
    public void getGoOutRecord(int wh_id) {
        if (wh_id > 0) {
            GoOutBean param = new GoOutBean();
            param.setWh_id(wh_id);
            //mPresenter.getGoOutRecordList(param);
        }

    }


    @Override
    protected void onSelectDate(int type, String content) {
        txtStartDate.setText(content);
    }


    private void showRecord(List<GoOutBean> result) {

        if (result == null || result.size() == 0) {
            txtNodata.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
        } else {
            txtNodata.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
            data.clear();
            data.addAll(result);
//            for (int i = 0; i < result.size(); i++) {
//                GetOutReccordBean bean = new GetOutReccordBean();
//                bean.setAmount(result.get(i).getAmount());
//                bean.setDate(result.get(i).getGoout_date());
//                bean.setOpreateTime(result.get(i).getOperate_time());
//                bean.setIndex(i + 1);
//                data.add(bean);
//            }
            if (adapter == null) {
                adapter = new GetOutReccordAdapter(R.layout.adapter_getout_reccord_item, data);
                recyclerview.setAdapter(adapter);
            } else {
                adapter.notifyDataSetChanged();
            }
        }

    }

    //出栏操作
    public void addGetOut() {
        //先判断鸽仓是否存在
        if (MainActivity.wh_id <= 0) {
            MyToast.showLong("鸽仓编号不正确，请选择正确鸽仓编号！");
            return;
        }

        String strAmount = edtGetoutAmount.getText().toString();
        if (strAmount.equals("") || strAmount == null) {
            MyToast.showLong("请添加出栏数量");
            return;
        }
        int amount = 0;
        try {
            amount = Integer.valueOf(strAmount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (amount == 0) {
            MyToast.showLong("请添正确数量");
            return;
        }
        String startDate = txtStartDate.getText().toString();
//        startDate += " " + DateTimeUtils.getCurrentTime();
//        long date = DateTimeUtils.date2TimeStamp(startDate);

        GoOutBean bean = new GoOutBean();
        bean.setGoout_date(startDate);
        bean.setAmount(amount);
        bean.setOperate_time(DateTimeUtils.timeStamp2DateTime(new Date().getTime()));
        bean.setWh_id(MainActivity.wh_id);
        mPresenter.addGoOut(bean);


    }

    @OnClick({R.id.txt_start_date, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_start_date:
//                selectStartValiDate();
                selectStartDate();
                break;
            case R.id.btn_add:
                addGetOut();
                break;
        }
    }

//    @Override
//    public void onAddGoOutSuccess(List<GoOutBean> list) {
//        showRecord(list);
//    }


    @Override
    public void onAddGoOutSuccess(BaseResponse response) {

    }

    @Override
    public void onGoOutListSuccess(PageInfo<GoOutBean> list) {

    }

    @Override
    public void onDeleteGoOutSuccess(BaseResponse response) {

    }

    @Override
    public void onFailed(String error) {

    }
}
